// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var BaseGame = require("./mzGameScript/BaseGame");
cc.Class({
    extends: BaseGame,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        //倒计时十位
        tenNode: {
            type: cc.Node,
            default: null,
        },

        //倒计时个位
        singleNode: {
            type: cc.Node,
            default: null,
        },
    },

    start() {
        this.countDownId = 0;
        this._super();
        this.sceneOver = false;
        this.noEatStep = 0;
    },

    onGenerateGameData() {
        let gameData = this._super();
        gameData.step = "black";
        return gameData;
    },

    //获取当前游戏所有数据, 以用于同步重新连接时的游戏数据
    onGetGameData() {
        let gameData = this._super();
        gameData.step = this.step;
        return gameData;
    },

    //由子类重写， 必须调用this._super(gameData); 
    loadGame(gameData) {
        if (this.isServer) {
            this.color = "black";
            this.colorAllocation("black", "white");
        } else {
            this.colorAllocation("white", "black");
            this.color = "white";
        }
        this.step = gameData.step;//黑方先手

        this._super(gameData);
    },

    //颜色分配
    colorAllocation(selfColor, rivalColor) {

    },

    //判断是否是轮到我下棋
    myStep() {
        return this.step == this.color;
    },

    changePlayer(gainScore) {
        if (!gainScore) {
            this.noEatStep++;

            if (this.noEatStep == 5 * 2) {
                this.toast("再走5回合无吃子或翻子就平局啦");
            } else if (this.noEatStep == 10 * 2) {
                this.drawn();
            }
        } else {
            this.noEatStep = 0;
        }


        if (this.step == "black") {
            this.step = "white";
        } else {
            this.step = "black";
        }

        if (this.myStep()) {
            this.haveForPeace = false;
        }

        this.onColor(this.step);
    },

    setNumber(node, number) {
        if (node) {
            cc.loader.loadRes("Texture/top/numbers-and-pointers/" + number, cc.SpriteFrame, function (err, spriteFrame) {
                if(this.sceneOver) {
                    cc.log("loadRes sceneOver");
                    return;
                }
                if (err) {
                    cc.log(err);
                } else {
                    node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                }
            }.bind(this));
        }
    },

    //倒计时
    countDown() {
        this.countDownId++;
        let countDownId = this.countDownId;
        let nextSeconds = function (restSeconds) {
            if (restSeconds < 0) {
                //倒计时之后3秒还没有操作， 则游戏结束
                if (this.countDownId == countDownId) {
                    if (this.myStep()) {
                        this.defeat();
                    } else {
                        this.victory();
                    }
                }
            } else if (countDownId == this.countDownId) {
                //消除上次的倒计时
                this.setNumber(this.tenNode, Math.floor(restSeconds / 10));
                this.setNumber(this.singleNode, restSeconds % 10);
                this.nextSecondsTimer = setTimeout(function () {
                    nextSeconds(restSeconds - 1);
                }, 900);
            }


        }.bind(this);
        nextSeconds(30);
    },

    onDestroy(){
        cc.log("游戏场景销毁 RedBlue onDestroy");
        this._super();
        this.sceneOver = true;
        if(this.nextSecondsTimer) {
            clearTimeout(this.nextSecondsTimer);
        }
    },

    //轮到color方下棋, 必须调用this._super(gameData);
    onColor(color) {
        this.countDown();
        if (this.rivalIsRobot && !this.myStep()) {
            this.onRobotStep();
        }
    },

    onRobotStep() {

    }
});
